﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XRPGLibrary;
using myRPG;
using System.IO;

namespace myRPG.GameScreens
{
    public class Scene1 : BaseGameState 
    {
        #region Field region//Texture2D为图片加载对象
        Texture2D backgroundImage;
        protected int timeSinceLastUpdate = 0;
        protected int millisecondsPerUpdate = 150;

        SpriteFont YaheiFont;
        string[] text;
        string tempDialog = "";
        int temp = 0;
        int count = 0;
        Point imageSize;
        Texture2D textureImage;

        #endregion
        #region Constructor region
        public Scene1(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }
        
        #endregion
        #region XNA Method region
        protected override void LoadContent()
        {
            ContentManager Content = GameRef.Content;
            backgroundImage = Content.Load<Texture2D>(@"scene1\park");//背景图片加载
            StreamReader dailogCileReader = new StreamReader(@"../../../data/dialog0.dat", System.Text.Encoding.GetEncoding("gb2312"));
            string textureImageName = dailogCileReader.ReadLine();
            //textureImage = GameRef.Content.Load<Texture2D>(@textureImageName);
            imageSize.X = int.Parse(dailogCileReader.ReadLine());
            imageSize.Y = int.Parse(dailogCileReader.ReadLine());
            count = int.Parse(dailogCileReader.ReadLine());
            int num = 0;
            text = new string[count];
            while (num < count)
            {
                tempDialog = dailogCileReader.ReadLine();
                if (tempDialog != null && tempDialog != "")
                    text[num++] = tempDialog;
            }
            dailogCileReader.Close();

            YaheiFont = GameRef.Content.Load<SpriteFont>(@"Fonts/YaheiFont");
            //dialogBackGroundImage = game.Content.Load<Texture2D>(@"Images/dialogBox");
            //backGroundSize = new Point(608, 112);
            
            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            timeSinceLastUpdate += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastUpdate > millisecondsPerUpdate)
            {
                KeyboardState keyboard = Keyboard.GetState();
                if (keyboard.IsKeyDown(Keys.Space))
                {
                    temp++;
                    timeSinceLastUpdate = 0;
                }
                if (temp >= count)
                {
                    temp = 0;
                    //dialogVisable = false;
                    //nextMapVisable = true;
                }
            }
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            //绘制背景图片，注意绘制操作都是在Begin()和End()操作之间
            GameRef.SpriteBatch.Begin();
            base.Draw(gameTime);
            GameRef.SpriteBatch.Draw(
            backgroundImage, new Vector2(0, imageSize.Y),
                new Rectangle(0, 0, 1024, 768),
                Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0.9f);
           // GameRef.SpriteBatch.DrawString(YaheiFont, text[temp], new Vector2(64, 280), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1.0f);
            GameRef.SpriteBatch.End();
        }
        #endregion
    }
}